<?php


namespace App\Models;


use App\Services\ZlbService;
use Illuminate\Database\Eloquent\Model;

class ZlbOrderGoods extends Model
{
    protected $table = "order_goods";
    protected $primaryKey = 'id';
    public $timestamps = false;

    /**
     * 订单商品上链新增数据
     * @param $order
     * @param $orderGoods
     * @param $goods
     * @param $price
     */
    public static function add($order,$orderGoods,$goods,$price){
        $data = [
            'order_no'=>$order['trade_no'],
            'goods_id'=>$orderGoods['goods_id'],
            'goods_title'=>$orderGoods['goods_title'],
            'goods_img'=>$goods['goods_img'],
            'shop_id'=>$order['seller_id'],
            'amount'=>$order['amount'],
            'create_time'=>dateFormat(time()),
            'goods_version_id'=>$goods['version_id'],
            'price'=>$price,
            'goods_relation_id'=>$goods['id'],
        ];
        ZlbOrderGoods::insertGetId($data);
    }

    /**
     * 订单商品上链
     * @param $order
     * @param $request
     * @param $notary
     */
    public static function set($order,$request,$notary){
        $res = ZlbService::format($notary);
        if($res['status'] == ZlbService::STATUS_YES){
            //成功
            ZlbService::requestLog('上传订单商品成功',$request,$res);
        }else{
            //失败
            ZlbService::requestLog('上传订单商品失败',$request,$res);
        }
        $data = [
            'tx_hash'=>$res['tx_hash'],
            'chain_status'=>$res['status'],
            'chain_time'=>dateFormat(time()),
            'chain_params'=>ZlbService::paramsResponse($request,$notary),
        ];
        ZlbOrderGoods::where(['order_no'=>$order['trade_no']])->update($data);
    }
}
